package com.gdpa.controller;


import com.gdpa.entity.DistrBuilding;
import com.gdpa.entity.DistrHouse;
import com.gdpa.service.IDistrHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 分销房源表 前端控制器
 * </p>
 *
 * @author zsr
 * @since 2018-12-19
 */
@RestController
@RequestMapping("/distr-house")
public class DistrHouseController {
    @Autowired
    IDistrHouseService houseService;

    @RequestMapping("/toHouse")
    public ModelAndView toHouse(String data, HttpServletRequest request){
        List<DistrHouse> list = houseService.findAllHouse(data);
        DistrBuilding build = houseService.findNowBuild(data);
        if(list != null){
            request.setAttribute("list",list);
        }
        if (build != null){
            request.setAttribute("build",build);
            request.getSession().setAttribute("building",build);
        }
        ModelAndView mv = new ModelAndView("distr_house/house_manage");
        return mv;
    }
    @RequestMapping("/addHouse")
    public ModelAndView addHouse(DistrHouse house){
        System.out.println(house);
        if (house != null){
            houseService.addHouse(house);
        }
        ModelAndView mv = new ModelAndView("redirect:toHouse?data="+house.getDistrBuildingId());
        return mv;
    }
    @RequestMapping("/findHouse")
    public ModelAndView findHouse(DistrHouse house,HttpServletRequest request){
        System.out.println(house);
        List<DistrHouse> list = houseService.findHouse(house);
        DistrBuilding build = houseService.findNowBuild(house.getDistrBuildingId().toString());
        if(list != null){
            request.setAttribute("list",list);
        }
        if (build != null){
            request.setAttribute("build",build);
        }
        ModelAndView mv = new ModelAndView("distr_house/house_manage");
        return mv;
    }
    @RequestMapping("/deleteHouse")
    public ModelAndView deleteHouse(String data,HttpServletRequest request){
        houseService.deleteHouse(data);
        DistrBuilding build = (DistrBuilding)request.getSession().getAttribute("building");
        ModelAndView mv = new ModelAndView("redirect:toHouse?data="+build.getDistrBuildingId());
        return mv;
    }

}
